<?php

	class SubMenu{
		
		public $id;		
		public $descHtml;		
		public $menu;		
		public $urlIcone;		
		public $endPasta =  "../../img/buttonsMenu/";
		public $arquivo;		
		public $urls;		
		public $errosUpload;
			
		public function getId(){
			
			return $this->id;
			
		}
		
		public function setId($id){
			
			$this->id = $id;
			
		}
		
		public function getDescHtml(){
			
			return $this->descHtml;
			
		}
		
		public function setDescHtml($descHtml){
			
			$this->descHtml= $descHtml;
			
		}
		
		public function setUrlIcone($urlIcone){
			
			$this->urlIcone = $urlIcone;
			
		}
		
		public function getUrlIcone(){
			
			return $this->urlIcone;
			
		}
		
		public function getMenu(){
			
			return $this->menu;
			
		}
		
		public function setMenu($menu){
			
			$this->menu = $menu;
			
		}
		
		public function getArquivo(){
			
			return $this->arquivo;
			
		}
		
		public function setArquivo($arquivo){
			
			$this->arquivo = $arquivo;
			
		}
		
		public function setUrls($urls){
			
			$this->urls = $urls;	
			
		}
		
		public function getUrls(){
			
			return $this->urls;
			
		}
		public function verificaPermissaoUsuario($tipoUsuario,$idSubMenu){
			$db = new conexao();
			$db->abrir();
			$sql = "select * from tipo_usuario_submenu tus
					where tus.sub_id=".$idSubMenu."
					and tus.tip_use_id=".$tipoUsuario->getId();
			
			$resultSet = $db->consulta($sql);
			$linha = $db->get_linha($resultSet);
			if($linha!=null){
				return true;
			}else{
				return false;
			}			
		}
		public function inserir(){
			
			$gerenteUpload = new GerenteUpload();
			$gerenteUpload->endereco = $this->endPasta;
			
			if($gerenteUpload->uploadArquivo($this->getArquivo())){
				
				$this->setUrlIcone($gerenteUpload->nomeArquivo);
				$db = new conexao();
				$db->abrir();
				
				$sql ="INSERT INTO `sub_menu` (`sub_html`, `sub_urlimg`, `men_id`) VALUES 
					   ('".$this->getDescHtml()."','".$this->getUrlIcone()."',".$this->menu->getId().");";
				
				if($db->inserir($sql)){
					
					$this->setId($db->get_ult_id());
					$submenu = new SubMenu();
					$submenu->setId($this->getId());
					if($this->getUrls()!=null){
						foreach ( $this->getUrls() as $url ) {
							
							$url->setSubMenu($submenu);
							
			       			if(!$url->inserir()){
			       				
			       				return false;
			       				
			       			}
			       			
						}
					}
					
					return true;
					
				}
				
				else{
					return false;
					
				}
				
			}
			
			else{
				
				$this->errosUpload =  $gerenteUpload->erro;
				return false;
				
			}
				
		}
		
		public function verificaSubmenuExistente(){
			$db = new conexao();
			$db->abrir();
			$sql ="select count(*) as quant from sub_menu where sub_html ='".$this->getDescHtml()."';";
			$result = $db->consulta($sql);
			$resp = 0;
			while($linha = $db->get_linha($result)){
				$resp = $linha["quant"];
			}
			
			return $resp;
		}
		
		public function checkBoxSet($codigo=""){

			$consultar = "";
			$sql =" select *from `sub_menu` order by `sub_menu`.`sub_html`";

			$db = new conexao();
			$db->abrir();

			$resultado = $db->consulta($sql);

			while($linha = $db->get_linha($resultado)){

				$consultar.="<input type='checbox' value = ".$linha['sub_id']." ";

				if($codigo!="" && $codigo == $linha["sub_id"]){

					$consultar.=" selected ";

				}

				$consultar.=">";

			}
			
			$db->fechar();
			return $consultar;
		}
		public function getListaSubMenusForDesc($descSubMenu,$association){
			$db = new conexao();
			$db->abrir();
			$sql = "";
			$listaMenus =  array();
			if(!$association){
				$sql = "select * from sub_menu ";
				$sql .= "where `sub_menu`.`sub_html`like'%".$descSubMenu."%'";
			}else{
				$sql = "select * from sub_menu sub
						inner join menu me on me.men_id = sub.men_id
						where sub.sub_html like'%".$descSubMenu."%'";
				
			}
			
			$resultSet =  $db->consulta($sql);
			while($linha = $db->get_linha($resultSet)){
				$listaMenus[] = $this->parseResultSetForObj($linha,new SubMenu(),$association);
			}
			$db->fechar();
			return $listaMenus;
		}
		public function getListaSubMenuForTipoUsuario($tipoUsuario,$db=null){
			$criaConexao=false;
			$lista=array();
			if($db==null){
				$db = new conexao();
				$db->abrir();
				$criaConexao = true;
			}
			$sql ="select sub.* from tipo_usuario_submenu tus
					inner  join sub_menu  sub on sub.sub_id = tus.sub_id
					where tus.tip_use_id =".$tipoUsuario->getId();
			
			$resultSet = $db->consulta($sql);
			while($linha= $db->get_linha($resultSet)){
				
				$lista[] = $this->parseResultSetForObj($linha, new SubMenu(),false);
			}
			if($criaConexao){
				$db->fechar();
			}
			

			return $lista;
		
		}
		public function parseResultSetForObj($linha,$obj,$association){		
			if(!$association){
				$obj->setId($linha["sub_id"]);
				$obj->setDescHtml($linha["sub_html"]);
				$obj->setUrls($linha["sub_urlimg"]);
				$menu = new Menu();
				$menu->setId($linha["men_id"]);
				$obj->setMenu($menu);
			}else{
				$obj->setId($linha["sub_id"]);
				$obj->setDescHtml($linha["sub_html"]);
				$obj->setUrls($linha["sub_urlimg"]);
				$menu = new Menu();
				$menu->setDescHTML($linha["men_html"]);
				$menu->setDescMenu($linha["men_desc"]);
				$menu->setId($linha["men_id"]);
				$obj->setMenu($menu);
			}	
			return $obj;
		
		}
		public function checkBox($nome,$id){
			
			$gerenteHtml = new GerenteHTML();
			return $gerenteHtml->checkBox("sub_menu","sub_html","sub_id",$nome,$id,5);
			
		}
		
	}
?>
